package com.xu.sys.servlet;

import com.xu.sys.bean.SysRole;
import com.xu.sys.bean.SysUser;
import com.xu.sys.service.IRoleService;
import com.xu.sys.service.IUserService;
import com.xu.sys.service.impl.IUserServiceimpl;
import com.xu.sys.service.impl.RoleServiceimpl;
import com.xu.sys.utils.Constant;
import com.xu.sys.utils.PageUtils;
import com.xu.sys.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "userServlet",urlPatterns = {"/sys/userServlet"})
public class UserServlet extends BaseServlet {
    private IUserService service = new IUserServiceimpl();
    private IRoleService roleService = new RoleServiceimpl();

    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        super.list(req,resp); // 调用父类中的方法完成分页数据的处理
// 做分页的查询
        service.listPage(pageUtils);
// 把查询的用户数据存储在 request作用域中
        req.setAttribute("pageUtils",pageUtils);
// 通过服务端转发的方式跳转页面
        req.getRequestDispatcher("/sys/user/list.jsp").forward(req, resp);
    }

    @Override
    public void saveOrUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //查询出所有角色信息
        List<SysRole> roles = roleService.list(null);
        req.setAttribute("roles",roles);

        //表示需要进入addOrUpdatePage.jsp界面
        String id = req.getParameter("id");
        if(StringUtils.isNotEmpty(id)){
            //更新操作
            SysUser user = service.findById(Integer.parseInt(id));
            req.setAttribute("entity",user);
        }
        req.getRequestDispatcher("/sys/user/addOrUpdate.jsp").forward(req, resp);
    }


    @Override
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //表示添加数据
        String  id = req.getParameter("id");
        String  username = req.getParameter("username");
        String  password = req.getParameter("password");
        String  nickname = req.getParameter("nickname");
        String  img = req.getParameter("img");
        String roleId = req.getParameter("roleId");
        SysUser user = new SysUser();
        if (StringUtils.isNotEmpty(roleId)){
            //根据角色id查询对应的信息
            SysRole role = roleService.findById(Integer.parseInt(roleId));
            user.setRoleId(Integer.parseInt(roleId));
            user.setRolename(role.getName());
        }
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        user.setImg(img);
        if(StringUtils.isNotEmpty(id)){
            //更新数据
            user.setId(Integer.parseInt(id));
            service.updteById(user);//更新用户信息


        }else {
            // 调用Service的方法完成数据的存储
            service.save(user);
        }

        //数据查询
        resp.sendRedirect("/sys/userServlet?action="+ Constant.BASE_ACTION_LIST);
    }

    @Override
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        int count = service.deleteById(Integer.parseInt(id));
        PrintWriter writer = resp.getWriter();
        writer.write(count+"");
        writer.flush();
        writer.close();


    }

    @Override
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }


}
